﻿using NPOI.OpenXmlFormats.Wordprocessing;
using Quartz;
using System;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.JobExcutorDomain;

namespace Zocono.WCS.HangfireHost.TaskJobs
{
    /// <summary>
    /// 读取DB后解析报文处理业务
    /// </summary>
    public class RepeatReadDbJob
    {
        private readonly IJobExcutorService _jobExcutorService;
        private readonly ILogUtil _logUtil;
        private readonly TaskLock _taskLock;

        public RepeatReadDbJob(IJobExcutorService jobExcutorService,
            ILogUtil logUtil,
            TaskLock taskLock)
        {
            _jobExcutorService = jobExcutorService;
            _logUtil = logUtil;
            _taskLock = taskLock;
        }

        /// <summary>
        /// 执行作业任务
        /// </summary>
        /// <returns></returns>
        public async Task DoJob(string programeCode, string jobName)
        {
            //if (_taskLock.ReadPlcTaskLock)
            //{
            //    return;
            //}
            try
            {
                //_taskLock.ReadPlcTaskLock = true;
                //读取PLC DB数据后处理业务
                await _jobExcutorService.ReadPlcDbJobAsync(programeCode, jobName);
            }
            catch (Exception ex)
            {
                _logUtil.Error($"PLC读取任务报错ex={ex}");
            }
            finally
            {
                //_taskLock.ReadPlcTaskLock = false;
                _logUtil.Debug($"PLC读取任务完成");
            }
        }

        /// <summary>
        /// 使用hangfire时调用
        /// </summary>
        /// <returns></returns>
        public async Task HangfireJob(string programeCode, string jobName)
        {
            await DoJob(programeCode, jobName);
        }

    }
}
